package com.zxf.android.mobilesafe.adapter;

import java.util.List;

import com.zxf.android.mobilesafe.R;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyAppAdapter extends BaseAdapter {
	private Context context;
	private List<PackageInfo> appList;
	private PackageManager packageManager;
	ViewHolder holder = new ViewHolder();
	static class ViewHolder{
		ImageView appIcon;
		TextView appName;
		TextView appVersion;
		TextView packageName;		
	}

	public MyAppAdapter(Context context, List appList) {
		 	this.context = context;
		 	this.appList = appList;
		 	packageManager = context.getPackageManager();
	}

	@Override
	public int getCount() {
		return appList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return appList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		if(convertView == null) {
			convertView = View.inflate(context, R.layout.list_item_appmanager, null);
		}
		holder.appIcon = (ImageView) convertView.findViewById(R.id.iv_app_icon);
		holder.appName = (TextView) convertView.findViewById(R.id.tv_app_name);
		holder.appVersion = (TextView) convertView.findViewById(R.id.tv_app_version);
		holder.packageName = (TextView) convertView.findViewById(R.id.tv_app_packageName);
		Drawable drawable = appList.get(position).applicationInfo.loadIcon(packageManager);
		holder.appIcon.setImageDrawable(drawable);
		holder.appName.setText(appList.get(position).applicationInfo.loadLabel(packageManager));
		holder.packageName.setText(appList.get(position).packageName);
		holder.appVersion.setText(appList.get(position).versionName);
		return convertView;
	}
	
	

}
